'use strict';

/**
 * 组织机构管理控制器
 */
const Controller = require('egg').Controller;

class DepartmentController extends Controller {
  /**
   * 创建部门
   * @return {Promise<void>}
   */
  async create() {
    const { ctx, service } = this;
    const departmentData = ctx.request.body;
    const result = await service.department.create(departmentData);
    ctx.success(result);
  }

  /**
   * 查询部门列表
   * @return {Promise<void>}
   */
  async index() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.department.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 查询部门列表
   * @return {Promise<void>}
   */
  async list() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.department.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 查询部门树
   * @return {Promise<void>}
   */
  async tree() {
    const { ctx, service } = this;
    const result = await service.department.tree();
    ctx.success(result);
  }

  /**
   * 查询单个部门
   * @return {Promise<void>}
   */
  async show() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const result = await service.department.findById(id);
    ctx.success(result);
  }

  /**
   * 更新部门
   * @return {Promise<void>}
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const departmentData = ctx.request.body;
    const result = await service.department.update(id, departmentData);
    ctx.success(result);
  }

  /**
   * 删除部门
   * @return {Promise<void>}
   */
  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.department.destroy(id);
    ctx.success(null, '删除成功');
  }

  /**
   * 更新部门状态
   * @return {Promise<void>}
   */
  async updateStatus() {
    const { ctx, service } = this;
    const { id, status } = ctx.request.body;
    await service.department.updateStatus(id, status);
    ctx.success(null, '状态更新成功');
  }
}

module.exports = DepartmentController;
